import { KS } from '../../Core/Platform/kuaishouSDK/kuaishou';
import { Meizu } from '../../Core/Platform/meizuSDK/meizu';
import { OPPO } from '../../Core/Platform/oppoSDK/oppoAdMange';
import { Toutiao } from '../../Core/Platform/toutiao/toutiao';
import { Vivo } from '../../Core/Platform/vivoSDK/Vivo';
import { Wechat } from '../../Core/Platform/wechat/wechat';
import { XM } from '../../Core/Platform/xiaomiSDK/xiaomi';
import TyqEventMgr from '../../tyq/tyq-event-mgr';
import tyqExtend from '../../tyq/tyq_extend';
import { JXDef } from './../../conventions/JXCommon';
import { ObjectWrap } from './../../Core/FrameEx/ES5Ex';
import { Channel } from './../Common/Define';
import GameMgr from './GameMgr';

export interface IUserInfo {
    nickName: string;
    avatar: string;
    sex?: number,
}
export default class SdkMgr extends ObjectWrap {
    protected static _ins: SdkMgr;

    /**录屏对象 */
    private _mediaRecorder: any = null;
    public static get ins(): SdkMgr {
        if (!this._ins) {
            this._ins = new SdkMgr;
        }
        return this._ins;
    }

    constructor() {
        super();
        if (cc.sys.platform == cc.sys.BYTEDANCE_GAME) {
            this.channel = Channel.TT;
        } else if (window.kwaigame) {
            this.channel = Channel.KS;
        } else if (cc.sys.platform == cc.sys.WECHAT_GAME && window.wx) {
            this.channel = Channel.WECHAT;
        } else if (cc.sys.isMobile && cc.sys.isBrowser) {
            this.channel == Channel.DEFAULT;
        } else if (CC_PREVIEW) {
            this.channel == Channel.DEFAULT;
        }
    }

    private channel: Channel = Channel.DEFAULT;
    private _videoRes: string = "";
    public getChannel() {
        return this.channel;
    }

    /**观看广告 */
    public watchAd(cb: Function, fails?: Function) {
        switch (this.channel) {
            case Channel.DEFAULT: {
                cb();
                GameMgr.lUserData.addAdWatchTime();
                break;
            }
            case Channel.TT: {
                Toutiao.showRewardVideoAd("guanggao").then(() => {
                    console.log("success")
                    GameMgr.lUserData.addAdWatchTime();
                    cb();
                }, (res) => {
                    fails()
                    this.umaSetPoint(JXDef.umaPoint.watchAdErr, { msg: res })
                    console.log("fail")
                    GameMgr.uiMgr.showToast("广告加载失败");
                })
                break;
            }
            case Channel.WECHAT: {

                Wechat.showRewardVideoAd().then((isSuc) => {
                    if (isSuc) {
                        console.log("success")
                        cb();
                    }

                }, (res?) => {
                    fails()
                    console.log(res)
                    console.log("fail")
                })
                break;
            }
            case Channel.MEIZU: {
                Meizu.showRewardedVideoAd().then(() => {
                    console.log("success")
                    cb();
                }, () => {
                    fails()
                    console.log("fail")
                })
                break;
            }
            case Channel.OPPO: {
                OPPO.showRewardVideoAd(() => {
                    console.log("success")
                    cb();
                }, () => {
                    fails()
                    console.log("fail")
                })
                break;
            }
            case Channel.VIVO: {
                Vivo.showRewardVideoAd((isSuc) => {
                    if (isSuc) {
                        console.log("success")
                        cb();
                    } else {
                        fails()
                        console.log("fail")
                    }
                })
                break;
            }
            case Channel.XIAOMI: {
                XM.showRewardedVideoAd(() => {
                    console.log("success");
                    cb();
                }, () => {
                    fails()
                    console.log("fail")
                })
                break;
            }
            case Channel.KS: {
                KS.showRewardedVideoAd(() => {
                    cb();
                })
                break;
            }
        }

    }

    public umaSetPoint(event: string, obj?: any) {
        if (obj) {
            TyqEventMgr.ins.sendEvent(event, obj.msg);
        } else {
            TyqEventMgr.ins.sendEvent(event);
        }
    }

    public showMoreGamesModal() {
        switch (this.channel) {
            case Channel.TT: {
                Toutiao.showMoreGamesModal();
                break;
            }
        }
    }

    public ttFocusOn() {
        Toutiao.openAwemeUserProfile();
    }

    /**录屏开始 */
    protected startRecoding() {

    }

    /**事件添加开始录屏 */
    public recordStart() {
        console.log("录屏开始*--*");
        if (this.channel == Channel.TT) {
            Toutiao.startRecordScreen(300, this.startRecoding);
            console.log("录屏开始11111", 11111);
        } else if (this.channel == Channel.KS) {
            this._mediaRecorder = kwaigame.createMediaRecorder();
            this._mediaRecorder.start();
        } else if (this.channel == Channel.WECHAT) {

        }
    }

    public puaseRecord() {
        console.log("暂停录屏");
        if (this.channel == Channel.TT) {
            Toutiao.pauseRecordScreen();
        } else if (this.channel == Channel.KS) {
            this._mediaRecorder.pause();
        }
    }

    public resumeRecord() {
        console.log("继续录屏")
        if (this.channel === Channel.TT) {
            Toutiao.resumeRecordScreen()
        } else if (this.channel == Channel.KS) {
            this._mediaRecorder.resume();
        }
    }

    /**事件添加结束录屏 */
    public recordEnd() {
        console.log("录屏结束*--*");
        if (this.channel == Channel.TT) {
            Toutiao.stopRecordScreen((path) => {
                Toutiao.clipVideo(60, path, (res) => {
                    this._videoRes = res.videoPath;
                    console.log("录屏结束*--*", 11111);
                })
            });
        } else if (this.channel == Channel.KS) {
            this._mediaRecorder.stop();
        }
    }

    /**事件转发 */
    public recordCut(cb: Function) {
        if (this.channel == Channel.TT) {
            console.log("录屏" + this._videoRes);
            Toutiao.shareAppMessage("象棋保卫战", this._videoRes, ["#象棋保卫战", "#抖音小游戏", "#塔防", "#象棋"]).then(() => {
                console.log('分享视频成功');
                cb();
                this.umaSetPoint(JXDef.umaPoint.shareRecord)
            })
        } else if (this.channel == Channel.KS) {
            this._mediaRecorder.publishVideo({
            });
        } else {
            console.log("假装分享视频成功")
            cb();
        }
    }

    //授权
    public authorize(cb: (userInfo: IUserInfo | null) => void) {
        if (this.channel == Channel.TT) {
            Toutiao.TTLogin(() => {

                Toutiao.toutiaoLogin().then((res: any) => {
                    if (res) {
                        let userInfo: IUserInfo = {
                            nickName: res.name,
                            avatar: res.photo,
                        }
                        cb(userInfo);
                        //获取抖音的oppenid
                        tyqExtend.getBytedanceOpenid(res.code, res.anonymousCode);
                    } else {
                        cb(null);
                    }


                }).catch((res) => {
                    console.log(`toutiaoLogin 调用失败`);
                    console.log(res);
                    cb(null);
                })
            }, (res) => {
                cc.log(res);
                cb(null);
            }, false)
        } else if (this.channel == Channel.KS) {
            KS.KSlogin(() => {
                KS.KSgetUserInfo((res: any) => {

                    let userInfo: IUserInfo = {
                        nickName: res.userName,
                        avatar: res.userHead,
                    }
                    cb(userInfo);
                })
            })
        } else if (this.channel == Channel.DEFAULT) {
            cb(null);
        } else if (this.channel == Channel.WECHAT) {
            // Wechat.login(() => {

            //     Wechat.getUserInfo((res: any) => {
            //         let userInfo: IUserInfo = {
            //             nickName: res.userInfo.nickName,
            //             avatar: res.userInfo.avatarUrl,
            //         }
            //         cb(userInfo);
            //     }, (res) => {
            //         console.log(`getUserInfo 调用失败`);
            //         console.log(res);
            //         cb(null);
            //     })
            // }, (res) => {
            //     cc.log(res);
            //     cb(null);
            // 
        }
        //授权
    }
    public initGame() {

    }

    public loginOut() {
    }
    /**是否显示分享录屏按钮 */
    public isShowRecordBtn() {
        if (this.channel == Channel.TT || this.channel == Channel.KS) {
            return true;
        }
        return false;
    }



    public isWechat() {
        return this.channel == Channel.WECHAT;
    }



}